Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Incorrect use of partial in TweedieDistribution._rowwise_gradient_hessian #889

Merged
merged 11 commits into from
Jan 9, 2025

Conversation

bili2002
Copy link
Contributor

This PR addresses issue #888 by fixing the incorrect use of partial in the TweedieDistribution._rowwise_gradient_hessian function.

Checklist

  • Added a CHANGELOG.rst entry

Copy link
Collaborator

@stanmart stanmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for finding fixing this! I believe there is a similar mistake here:

f = partial(inv_gaussian_log_eta_mu_deviance, p=self.power)

Could you please address it too so that they are in the same PR?

Also, I think it'd be worth adding a test case that would have spotted these bugs (maybe here?), but I'm also o kay with doing it later.

@stanmart
Copy link
Collaborator

Hm, it might be a bit more involved than that. Is it possible that inv_gaussian_log_rowwise_gradient_hessian is incorrect? Shouldn't it be

@@ -276,12 +276,12 @@ def inv_gaussian_log_rowwise_gradient_hessian(
         inv_mu = 1 / mu[i]
         inv_mu2 = inv_mu ** 2
 
-        gradient_rows_out[i] = 2 * weights[i] * (inv_mu - y[i] * inv_mu2)
-        hessian_rows_out[i] = 2 * weights[i] * (inv_mu - 2 * y[i] * inv_mu2)
+        gradient_rows_out[i] = weights[i] * (y[i] * inv_mu2 - inv_mu)
+        hessian_rows_out[i] = weights[i] * (2 * y[i] * inv_mu2 - inv_mu)

instead?

Copy link
Collaborator

@stanmart stanmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you :)

@stanmart
Copy link
Collaborator

stanmart commented Jan 8, 2025

@bili2002, do you mind if I push the fix for those formulas to this branch before merging this PR?

@bili2002
Copy link
Contributor Author

bili2002 commented Jan 9, 2025

@stanmart Sure, go ahead with the formulas!

@stanmart
Copy link
Collaborator

stanmart commented Jan 9, 2025

@lbittarello, could you please check if my changes make sense?

@stanmart
Copy link
Collaborator

stanmart commented Jan 9, 2025

Also, if it's correct, I believe the relevant part of quantco/objectives needs to be updated, too.

CHANGELOG.rst Outdated Show resolved Hide resolved
src/glum/_functions.pyx Show resolved Hide resolved
stanmart and others added 3 commits January 9, 2025 11:24
Co-authored-by: Luca Bittarello <15511539+lbittarello@users.noreply.github.com>
Comment on lines +1308 to +1309
assert_allclose(glm.intercept_, glmnet_intercept, rtol=1e-3)
assert_allclose(glm.coef_, glmnet_coef, rtol=1e-3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what happens if you use the full Hessian? Do the tests pass? Are the coefficients closer to glmnet's?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The glmnet test passes and the coefficients are roughly as close as in the other case. Another test (tests/glm/test_glm.py::test_glm_family_argument[inverse.gaussian-fam4]) fails with a singular matrix LinalgError, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failing test might not be an issue though as I believe the model was originally meant to be penalized, which but it is not since glum v3. 🤔 Anyways, I think it needs a bit more thinking and testing if we want to go with the true Hessian. I'd vote for merging this fix relatively soon, and possibly switching to the true Hessian later on if we convince ourselves that it works.

@stanmart stanmart merged commit 3e483b9 into main Jan 9, 2025
24 checks passed
@stanmart stanmart deleted the fix-inverse-gaussian-call branch January 9, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants